var express = require('express');
var router = express.Router();
var session = require('express-session');
var moment = require('moment');

var db = require('../../utils/db.js');


// 获取版本信息列表
router.get('/listAll', function (req, res, next) {
            res.render('process/feedBack/index');
});

/**
 * 根据项目获取客户反馈列表
 */
router.get('/listByFeedBack', function (req, res, next) {
    var id = req.session.projectId;
    var sql = "select * from project_progress_feed_back where project_base_id=" + id;
    db.query(sql, function (err, rows) {
        if (rows && rows.length >= 0) {
            for (var i = 0; i < rows.length; i++) {
                rows[i].feed_back_time = moment(rows[i].feed_back_time).format('YYYY-MM-DD HH:mm:ss');
            }
            res.json({
                errcode: 0,
                message: '根据项目获取客户反馈成功',
                data: rows
            });

        } else {
            res.json({
                errcode: 1,
                message: '根据项目获取客户反馈失败'
            });
        }

    });
});


/**
 * 反馈增加
 */
router.get('/add', function (req, res, next) {
    var projectId = req.session.projectId;
    var sql = "select * from project_base as p left join version_base as v on p.id=v.project_base_id where p.id = " + projectId + " ORDER BY v.version_release_time DESC limit 1";
    db.query(sql, function (err, rows) {
        if (rows && rows.length >= 0) {
            res.render('process/feedBack/edit', {
                id: '',
                title: '',
                time: '',
                person: '',
                file: ''
            });
        } else {
            res.render('error', {
                message: err.message,
                error: err
            });
        }

    });

});

/**
 * 修改反馈页面
 */
router.get('/edit/:id', function (req, res, next) {
    var id = req.params.id;
    var projectId = req.session.projectId;
    if (id) {
        var versionSql = "select * from project_base as p left join version_base as v on p.id=v.project_base_id where p.id = " + projectId + " ORDER BY v.version_release_time DESC limit 1";
        var sql = "select * from project_progress_feed_back where id = " + id;
    }

    db.query(versionSql, function (err, rows) {
        if (rows && rows.length > 0) {

            db.query(sql, function (err, datas) {
                if (datas && datas.length >= 0) {
                    for (var i = 0; i < datas.length; i++) {
                        datas[i].feed_back_time = moment(datas[i].feed_back_time).format('YYYY-MM-DD HH:mm:ss');
                    }
                    res.render('process/feedBack/edit', {
                        id: datas[0].id,
                        title: datas[0].feed_back_title,
                        time: datas[0].feed_back_time,
                        person: datas[0].feed_back_person,
                        file: datas[0].feed_back_upload_file
                    });
                } else {
                    res.render('process/feedBack/edit', {
                        id: '',
                        title: '',
                        time: '',
                        person: '',
                        file: ''
                    });
                }

            })
        }
    });

});

/**
 * 反馈更新
 */
router.post("/save", function (req, res, next) {
    var id = req.body.id;
    var projectId = req.session.projectId;
    var title = req.body.title ? req.body.title.trim() : '';
    var time = req.body.time ? req.body.time.trim() : '';
    var person = req.body.person ? req.body.person.trim() : '';
    var file = req.body.fileId ? req.body.fileId.trim() : '';
    var sql = '';
    if (id) {
         sql = "update project_progress_feed_back set feed_back_title = '" + title + "',feed_back_upload_file = '" + file + "',feed_back_time = '" + time + "',feed_back_person = '" + person + "',project_base_id = '" + projectId + "' where id = " + id;
    } else {
         sql = "insert into project_progress_feed_back(`feed_back_title`,`feed_back_upload_file`,`feed_back_time`,`feed_back_person`,`project_base_id`) values('" + title + "','" + file + "','" + time + "','" + person + "','" + projectId + "')";
    }
    db.query(sql, function (err, rows) {
        if (!err) {
            res.redirect("/process/feedBack/listAll");
        } else {
            res.json({
                errcode: 1,
                message: '保存失败'
            });
        }
    });


});

/**
 * 反馈删除
 */
router.post("/delete/:id", function (req, res, next) {
    var id = req.params.id;
    var sql = '';
    if (id) {
        sql = "delete from project_progress_feed_back where id=" + id;
        db.query(sql, function (err, rows) {
            if (!err) {
                res.json({
                    errcode: 0,
                    message: '删除成功'
                });
            } else {
                res.json({
                    errcode: 1,
                    message: '删除失败'
                });
            }
        });
    } else {
        res.json({
            errcode: 2,
            message: '系统错误'
        });
    }
});

// 新增用户页面
router.get('/text', function (req, res, next) {
    var projectId = req.session.projectId;
    var sql = "select * from project_base as p left join version_base as v on p.id=v.project_base_id where p.id = " + projectId + " ORDER BY v.version_release_time DESC limit 1";
    db.query(sql, function (err, rows) {
        if (rows && rows.length >= 0) {
            res.render('process/feedBack/text', {
                file: ''
            });
        } else {
            res.render('error', {
                message: err.message,
                error: err
            });
        }
    });
});

module.exports = router;
